﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class EmpChangeList : System.Web.UI.Page
    {
        EmpChangeBiz empChangeBiz = new EmpChangeBiz();
      
        string empCode = "";
        string bTm = "";
        string eTm = "";
        string changeTypeName = "";
        string deptID = "";

        string account = "";

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["EmpCode"] != null) empCode = Request.Params["EmpCode"].ToString().Trim();
            if (Request.Params["BTime"] != null) bTm = Request.Params["BTime"].ToString().Trim();
            if (Request.Params["ETime"] != null) eTm = Request.Params["ETime"].ToString().Trim();
            if (Request.Params["ChangeTypeName"] != null) changeTypeName = Request.Params["ChangeTypeName"].ToString().Trim();
            if (Request.QueryString["DeptID"] != null) deptID = Request.QueryString["DeptID"].ToString();

           
            
            if (!IsPostBack)
            {

                btnAdd.Attributes.Add("onclick", "javascript:ShowLink('EmpChangeAdd.aspx',800,550);");
                // JSUtility.AddReset(btnClear);
                if (deptID == "")
                    new DepartmentBiz().InitDropDownList(ddlDeptID, "0");//针对一层结构使用
                else
                    new DepartmentBiz().InitDropDownList(ddlDeptID, deptID);//针对一层结构使用
                if (empCode != "")
                {
                    txtSerWord.Text = empCode;
                    if (bTm != "") txtBtime.Text = bTm;
                    if (eTm != "") txtEtime.Text = eTm;
                    new DefDicBiz().InitDropDownList("Admin_Change_type", ddlChangeTypeName, changeTypeName);
                    BondGvList(empCode, bTm, eTm, changeTypeName, deptID);
                }
                else
                    new DefDicBiz().InitDropDownList("Admin_Change_type", ddlChangeTypeName, "");
            }
        }
        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpChangeList.aspx", RightChar.Ser))
            {
                JSUtility.Alert(this, "您没有查询权限！");
                return;
            }
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string changeTypeName = ddlChangeTypeName.SelectedItem.Text.Trim();
            if (ddlChangeTypeName.SelectedValue == "0") changeTypeName = "";
            string deptID = ddlDeptID.SelectedValue.ToString();


            if (serWord == "" && bTime == "" && eTime == "" && deptID =="0")
            {
                bTime = System.DateTime.Now.AddMonths(-2).ToString("yyyy-MM-dd");
                eTime = System.DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (StringUtility.GetDiffDays(bTime, eTime) > XmlUtility.GetNumFormConfig("daysOfSearch"))
            {
                JSUtility.Alert(this, "查询日期间隔不能大于" + XmlUtility.GetNumFormConfig("daysOfSearch") + "天！");
                return;
            }

            BondGvList(serWord, bTime, eTime, changeTypeName, deptID);
        }

        protected void BondGvList(string serWord, string bTime, string eTime, string changeTypeName,string deptID)
        {
            DataTable dt = empChangeBiz.GetLists(serWord, bTime, eTime, changeTypeName, deptID);
          
            if (dt.Rows.Count > 0)
            {
                lbAmount.Text = "人数合计:    " + dt.Rows.Count;
            }
            else
            {
                lbAmount.Text = "";
            }
                //DataRow dr = dt.NewRow();
                //dr[9] = "合计:";
                //dr[10] = dt.Rows.Count;
                //dt.Rows.Add(dr);
                gvList.DataSource = dt;
                gvList.DataBind();
            
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtBtime.Text = "";
            txtEtime.Text = "";
            txtSerWord.Text = "";
        }
        //根据gridview打印
        //               <asp:Button ID="btnPrint" runat="server" Text="打印" OnClick="btnPrint_Click" CssClass="ButtonCss" />
        //protected void btnPrint_Click(object sender, EventArgs e)
        //{
           
        //}
       
        protected void btnExcel_Click(object sender, EventArgs e)
        {
    
                if (!AccountBiz.CheckRight(account, "EmpChangeList.aspx", RightChar.Out))
                {
                    JSUtility.Alert(this, "您没有导出权限！");
                    return;
                }
        
            //ExcelUtility.GridViewToExcel(gvList, "EmpChange.xls");
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string changeTypeName = ddlChangeTypeName.SelectedItem.Text.Trim();
            if (ddlChangeTypeName.SelectedValue == "0") changeTypeName = "";
            string deptID = ddlDeptID.SelectedValue.ToString();

            DataTable dt = empChangeBiz.GetLists(serWord, bTime, eTime, changeTypeName, deptID);
            if (dt != null)
                ExcelUtility.ExportDsToXls(this, dt, "EmpChange.xls"); 
        
        }


        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F3F8F8'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
                HyperLink hlCode = (HyperLink)e.Row.Cells[1].FindControl("hlEmpCode");
                HyperLink hlName = (HyperLink)e.Row.Cells[1].FindControl("hlEmpName");
                string urlParam = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ChangeID"));
                hlCode.NavigateUrl = "javascript:ShowLink('EmpChangeAdd.aspx?ChangeID=" + urlParam + "',800,550)";
                hlName.NavigateUrl = "javascript:ShowLink('EmpChangeAdd.aspx?ChangeID=" + urlParam + "',800,550)"; 
            }
        }
    }
}